254A - Cards with Numbers - CodeForces Solution


constructive algorithms sortings *1200

Please click on ads to support us..

Python Code:

from collections import defaultdict
import sys

sys.stdin = open("input.txt", "r")
sys.stdout = open("output.txt", "w")

n = int(input())*2
a = [int(i) for i in input().split()]

c = defaultdict(list)
for i in range(n):
    c[a[i]].append(i+1)

ans = []
for [k, v] in c.items():
    l = len(v)
    if l % 2:
        print('-1')
        exit()
    else:
        for i in range(0, l-1, 2):
            ans.append(f'{v[i]} {v[i+1]}')
            
print('\n'.join(ans))

C++ Code:

#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include <iomanip>
using namespace std;
#define ll long long
ll gcd(ll a, ll b) { return ((b == 0) ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return (b / gcd(a, b)) * a; }
void hhh() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
}
const double EPS = 1e-7;
int main() {
    hhh();
    int N;
    cin >> N;
    vector<int>v(N*2);
    map<int, vector<int>>ma;
    for (int i = 0; i < 2*N; i++) {
        cin >> v[i];
        ma[v[i]].push_back(i + 1);
    }
    for (auto it : ma) {
        if (it.second.size() % 2 != 0) {
            cout << -1;
            return 0;
        }
    }
    for (auto it : ma) {
        for (int i = 0; i < it.second.size()-1; i+=2) {
            cout << it.second[i] << " " << it.second[i + 1] << "\n";
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets
1691B - Shoe Shuffling
1706A - Another String Minimization Problem
1695B - Circle Game
1702B - Polycarp Writes a String from Memory
1701A - Grass Field
489C - Given Length and Sum of Digits
886B - Vlad and Cafes
915A - Garden
356A - Knight Tournament
1330A - Dreamoon and Ranking Collection
1692B - All Distinct
1156C - Match Points